//
//  ViewController.m
//  UI23_UICollectionView
//
//  Created by Scott on 16/1/13.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "ViewController.h"
#import "CellForList.h"
#import "LayoutOfFirst.h"
#import "CollectionViewOfFirst.h"


@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) LayoutOfFirst *flowLayout;

@property (nonatomic, retain) CollectionViewOfFirst *collection;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self createCollectionView];
    

}





#pragma mark - 知识点1 UICollectionViewFlowLayout
- (void)createFlowLayout {
/** FlowLayout父类是UICollectionViewLayout, 负责布局. 
 * 
 * 详见 LayoutOfFirst类.
 */
    self.flowLayout = [[LayoutOfFirst alloc] init];
    
}


#pragma mark - 知识点2 UICollectionView
- (void)createCollectionView {
    
    [self createFlowLayout];
    
    // 创建对象.
    self.collection = [[CollectionViewOfFirst alloc] initWithFrame:SCREEN_BOUNDS collectionViewLayout:self.flowLayout];
    
    [self.view addSubview:self.collection];
    
    self.collection.backgroundColor = [UIColor whiteColor];
    
    self.collection.showsVerticalScrollIndicator = NO;
    
    // delegate
    self.collection.dataSource = self;
    self.collection.delegate = self;
    
    // 注册cell(collectionView只支持先注册,再取出).
//    [self.collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"pool1"];
    
    [self.collection registerClass:[CellForList class] forCellWithReuseIdentifier:@"pool1"];
    

    // collectionView 的分区头部尾部视图采用重用池机制.
    [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolForHeader"];
    
    
    [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"poolForFooter"];

    // 添加手势
    [self addLongPressGesture];
    
}


#pragma mark - 知识点3 Collect的Item实现移动.
/** collectionView上面加上长按手势. */
- (void)addLongPressGesture {
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    
    [self.collection addGestureRecognizer:longPress];
    
}

- (void)handleLongPress:(UILongPressGestureRecognizer *)longPress {
    
    // 根据长按手势的状态, 调用item的移动方法.
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:
        {

            // 根据手势在View上的点, 找到对应item的indexpath
           NSIndexPath *beginIndexPath = [self.collection indexPathForItemAtPoint:[longPress locationInView:self.collection]];
            
            // 如果没有点中item, 直接退出.
            if (beginIndexPath == nil) {
                return;
            }
            
            
            [self.collection beginInteractiveMovementForItemAtIndexPath:beginIndexPath];
            break;
        }
            
        case UIGestureRecognizerStateChanged:
        {
            
            // 随着手势移动, item位置不断更新.
            [self.collection updateInteractiveMovementTargetPosition:[longPress locationInView:self.collection]];
            
            break;
        }
            
        case UIGestureRecognizerStateEnded:
        {
            // 手势结束, item移动结束.
            [self.collection endInteractiveMovement];
         
            break;
        }
        default:
            
            [self.collection cancelInteractiveMovement];
            
            break;
    }

}



#pragma mark - CollectionView DataSource
/** 两个必须实现的协议方法(1/2) */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 20;
}

/** ...协议方法(2/2) */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellForList *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool1" forIndexPath:indexPath];
    
    // cell赋值.
    cell.backgroundColor = [UIColor redColor];
    
    
    return cell;
}

/** 返回每个分区的头部和尾部重用视图. */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    // 判断是头部
    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"poolForHeader" forIndexPath:indexPath];
        
        // 赋值
        header.backgroundColor = [UIColor greenColor];
        
        
        return header;
   
    } else {
        
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"poolForFooter" forIndexPath:indexPath];
        
        
        // 赋值
        footer.backgroundColor = [UIColor blueColor];
        
        return footer;
    }
}

/** 哪些item 可以移动. */
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

/** 移动item调用此方法. */
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
    // 更新数据源!!!
    
    
}


#pragma mark - CollectionView Delegate
/** 选中item之后,调用此方法. */
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"%ld, %ld", indexPath.section, indexPath.item);
}





#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
